#ifndef _THREAD_H_
#define _THREAD_H_

#include <iostream>
#include <pthread.h> 
#include <string>
#include <cstdio>
#include <cstring>
#include <functional>
using namespace std;

namespace ThreadModlue
{
    static uint32_t number = 1;
    
    class Thread
    {
        using func_t = function<void()>;
    private:
        void EnableDetach()
        {
            cout << "线程被分离了" << endl;
            _isdetach = true;
        }

        void EnableRunning()
        {
            _isrunning = true;
        }

        //类的成员函数默认包含this指针，static里面没有，所有传this指针
        static void* Routinue(void* args)
        {
            //获得this指针
            Thread* self = static_cast<Thread*>(args);
            self->EnableRunning();
            if(self->_isdetach)
                self->Detach();

            //设置线程的名字
            pthread_setname_np(self->_tid, self->_name.c_str());

            //回调处理
            self->_func();
            return nullptr;
        }

    public:
        Thread(func_t func)
            :_tid(0),
            _isdetach(false),
            _isrunning(false),
            res(nullptr),
            _func(func)
        {
            _name = "thread-" + to_string(number++);
        }

        void Detach()
        {
            if(_isdetach)
                return;
            if(_isrunning)
                pthread_detach(_tid);
            EnableDetach();
        }

        bool Start()
        {
            if(_isrunning)
            {
                return false;
            }

            int n = pthread_create(&_tid, nullptr,Routinue,this);
            if(n != 0)
            {
                cerr << "create thread error " <<  strerror(n) << endl;
                return false;
            }
            else
            {
                cout << _name << " create thread success" << endl;
                return true;
            }
        }

        bool Stop()
        {
            if(_isrunning)
            {
                int n = pthread_cancel(_tid);
                if(n != 0)
                {
                    cerr << "cancel thread error " << strerror(n) << endl;
                    return false;
                }
                else
                {
                    _isrunning = false;
                    cout << _name << " stop success" << endl;
                    return true;
                }
            }
            return false;
        }

        void Join()
        {
            if(_isdetach)
            {
                cout << "你的线程已经是分离的了" << endl;
                return;
            }

            int n = pthread_join(_tid, &res);
            if(n != 0)
            {
                cerr << "Join thread error " << strerror(n) << endl;
            }
            else
            {
                cout << "Join success" << endl;
            }
        }

        ~Thread()
        {

        }

    private:
        pthread_t _tid;
        string _name;
        bool _isdetach;
        bool _isrunning;
        void* res;
        func_t _func;
    };

}

#endif